package com.lanou.lyl.littlebear;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/4/13.
 */

public class DatabaseTool {

    private DbHelper dbHelper;

    private static class SingletonHolder {
        private static final DatabaseTool INSTANCE = new DatabaseTool();
    }

    public static DatabaseTool getInstance() {
        return SingletonHolder.INSTANCE;
    }


    private DatabaseTool() {
        //调用application 来获得 context
        dbHelper = new DbHelper(BearApplication.getContext());
    }

    public void addCar(CarEntity car) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        if (car.get_id() != 0) {
            contentValues.put(DbHelper._ID, car.get_id());
        }
        contentValues.put(DbHelper.Car_Name, car.getName());
        contentValues.put(DbHelper.Car_SELECTED, car.getSelected());
        contentValues.put(DbHelper.Car_MODEL, car.getModel());
        contentValues.put(DbHelper.Car_UUID, car.getUuid());

        database.insert(DbHelper.CAR_TABLE_NAME, null, contentValues);

        database.close();
    }

    public void removeCar(int id) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        String whereClause = DbHelper._ID + " = ?";
        String[] whereArgs = new String[]{String.valueOf(id)};
        database.delete(DbHelper.CAR_TABLE_NAME, whereClause, whereArgs);
        database.close();
    }

    public void updateCar(CarEntity car) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(DbHelper.Car_Name, car.getName());
        contentValues.put(DbHelper.Car_SELECTED, car.getSelected());
        contentValues.put(DbHelper.Car_MODEL, car.getModel());
        contentValues.put(DbHelper.Car_UUID, car.getUuid());

        String whereClause = DbHelper._ID + " = ?";
        String[] whereArgs = new String[]{String.valueOf(car.get_id())};

        database.update(DbHelper.CAR_TABLE_NAME, contentValues, whereClause, whereArgs);
        database.close();
    }

    public List<CarEntity> queryCarsAll(CarEntity carEntity) {
        List<CarEntity> cars = new ArrayList<>(5);

        SQLiteDatabase database = dbHelper.getWritableDatabase();
        Cursor cursor = database.query(DbHelper.CAR_TABLE_NAME, null, null, null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int indexId = cursor.getColumnIndex(DbHelper._ID);
            int indexName = cursor.getColumnIndex(DbHelper.Car_Name);
            int indexSelected = cursor.getColumnIndex(DbHelper.Car_SELECTED);
            int indexModel = cursor.getColumnIndex(DbHelper.Car_MODEL);
            int indexUUID = cursor.getColumnIndex(DbHelper.Car_UUID);
            do {
                int id = cursor.getInt(indexId);
                String name = cursor.getString(indexName);
                int selected = cursor.getInt(indexSelected);
                int medel = cursor.getInt(indexModel);
                String uuid = cursor.getString(indexUUID);

                carEntity.setName(name);
                carEntity.setSelected(selected);
                carEntity.setModel(medel);
                carEntity.setUuid(uuid);

                cars.add(carEntity);

            } while (cursor.moveToNext());

            database.close();
        }

        return cars;
    }

    //查询出当前选中的小车
    public CarEntity querySelectedCar() {

        SQLiteDatabase database = dbHelper.getWritableDatabase();

        String selection = DbHelper.Car_SELECTED + " = ?";
        String[] selectionArgs = new String[]{selection};

        Cursor cursor = database.query(DbHelper.CAR_TABLE_NAME, null, selection, selectionArgs, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {

            int indexId = cursor.getColumnIndex(DbHelper._ID);
            int indexName = cursor.getColumnIndex(DbHelper.Car_Name);
            int indexSelected = cursor.getColumnIndex(DbHelper.Car_SELECTED);
            int indexModel = cursor.getColumnIndex(DbHelper.Car_MODEL);
            int indexUUID = cursor.getColumnIndex(DbHelper.Car_UUID);

            int id = cursor.getInt(indexId);
            String name = cursor.getString(indexName);
            int selected = cursor.getInt(indexSelected);
            int medel = cursor.getInt(indexModel);
            String uuid = cursor.getString(indexUUID);

            CarEntity carEntity = new CarEntity(id);

            carEntity.setName(name);
            carEntity.setSelected(selected);
            carEntity.setModel(medel);
            carEntity.setUuid(uuid);

            cursor.close();

            return carEntity;
        }
        return null;

    }

    //更改选中的车辆 通过id更改
    public void changeSelectedCar(int id) {
        //先把原来选中的车辆设置为不选中
        CarEntity currentSelectedCar = querySelectedCar();
        currentSelectedCar.setSelected(0);
        updateCar(currentSelectedCar);
        //将新选中的车设置为选中的状态
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DbHelper.Car_SELECTED,1);

        String whereClause = DbHelper._ID + " = ?";
        String[] whereArgs = new String[]{String.valueOf(id)};

        database.update(DbHelper.CAR_TABLE_NAME,contentValues,whereClause,whereArgs);
        database.close();
    }

    //更改选中的车辆 通过对象更改 复写了上面
    public void changeSelectedCar(CarEntity newSelectedCar){
        //先把原来选中的车辆设置为不选中
        CarEntity currentSelectedCar = querySelectedCar();
        currentSelectedCar.setSelected(0);
        updateCar(currentSelectedCar);
        //将新选中的车设置为选中状态
        newSelectedCar.setSelected(1);
        updateCar(newSelectedCar);
    }
}
